import wiringpi as gpio
from digit_tube import numbers

ds = 11
stcp = 12
shcp = 13

gpio.wiringPiSetupPhys()
gpio.pinMode(ds, gpio.OUTPUT)
gpio.pinMode(stcp, gpio.OUTPUT)
gpio.pinMode(shcp, gpio.OUTPUT)

gpio.digitalWrite(stcp, gpio.LOW)
gpio.digitalWrite(shcp, gpio.LOW)

def set_bit_data(data):
	gpio.digitalWrite(ds, data)
	gpio.digitalWrite(shcp, gpio.LOW)
	gpio.digitalWrite(shcp, gpio.HIGH)

def show_digit(n):
	for d in numbers[str(n)][::-1]:
		set_bit_data(int(d))
	set_bit_data(0)
	gpio.digitalWrite(stcp, gpio.HIGH)
	gpio.digitalWrite(stcp, gpio.LOW)

def clear():
	for d in range(8):
		set_bit_data(0)
	gpio.digitalWrite(stcp, gpio.HIGH)
	gpio.digitalWrite(stcp, gpio.LOW)

if __name__ == '__main__':
	try:
		while True:
			n = input('请输入一个数字:')
			show_digit(n)
			gpio.delay(1000)
	except KeyboardInterrupt:
		clear()

